import { createRouter, createWebHistory } from "vue-router";
import Login from "@/views/Login.vue";
import Home from "@/views/Home.vue";
import LogOut from "@/views/LogOut.vue";
import FileManager from "@/views/FileManager.vue";
import { useAuthStore } from "@/store/authStore";

const routes = [
    { path: '/', redirect: '/login' }, // 默认重定向到登录
    { path: '/login', component: Login, meta: { requiresAuth: false } },
    {
        path: '/home',
        component: Home,
        meta: { requiresAuth: true },
        children: [
            { path: 'file-manager', component: FileManager },
            { path: 'logout', component: LogOut }
        ]
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

// 导航守卫：检查登录状态
router.beforeEach((to, from, next) => {
    const authStore = useAuthStore();
    if (to.meta.requiresAuth && !authStore.isAuthenticated) {
        next("/login");
    } else {
        next();
    }
});

export default router;